package cn.edu.dgut.sai;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author Sai
 * Created by Sai on 2019/8/26.
 *
 * 其它接口{@link org.springframework.beans.factory.Aware}
 * https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/core.html#aware-list
 */
@SuppressWarnings("unused")
//@Component("userBean")
//@Lazy
public class User implements InitializingBean, DisposableBean, ApplicationContextAware {
    private String name;
    private String sex;

    private ApplicationContext context;

    public User() {
        System.out.println("初始化");
        this.sex = "男";
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("User: destroy");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("男".equals(this.sex));
        System.out.println("afterPropertiesSet回调");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("applicationContext注入");
        this.context = applicationContext;
        Customer customerBean = (Customer)this.context.getBean("customerBean");
        System.out.println(customerBean);
    }
}
